package com.luxk.db.data;

import java.io.PrintStream;
import java.io.PrintWriter;

import com.luxk.db.block.AbstractBlock;
import com.luxk.db.block.BlockCacheHeader;
import com.luxk.db.block.BlockCacheHeaderHelper;
import com.luxk.db.block.BlockSpaceHeader;
import com.luxk.db.block.BlockSpaceHeaderHelper;
import com.luxk.db.block.BlockTxHeader;
import com.luxk.db.block.BlockTxHeaderHelper;
import com.luxk.db.data.undo.UndoAddress;
import com.luxk.db.tx.Xid;

public class TableBlock extends AbstractBlock implements BlockCacheHeader, 
BlockSpaceHeader, BlockTxHeader {
	
	/* format:
	 * cache header
	 * space header
	 * tx header
	 * table data header
	 * [row data]
	 * cache tail
	 */
	
	private static final int OFFSET_CACHE_HEADER = 0;
	
	@Override
	public void load(byte[] b, int offset, int size) {
		// TODO Auto-generated method stub
		
	}
	
/*= cache header 구현 =========================================================*/
	
	@Override
	public int getBlockCacheChecksum() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public BlockType getBlockType() {
		return BlockCacheHeaderHelper.getBlockType(this.buffer, 
				OFFSET_CACHE_HEADER);
	}

	@Override
	public void setBlockType(BlockType type) {
		BlockCacheHeaderHelper.setBlockType(this.buffer,  OFFSET_CACHE_HEADER,
				type);
	}

	@Override
	public BlockSize getBlockSizeType() {
		return BlockCacheHeaderHelper.getSizeType(this.buffer, 
				OFFSET_CACHE_HEADER);
	}

	@Override
	public void setBlockSizeType(BlockSize size) {
		BlockCacheHeaderHelper.setSizeType(this.buffer, OFFSET_CACHE_HEADER,
				size);
	}

	@Override
	public int getBlockCacheDba() throws Exception {
		return BlockCacheHeaderHelper.getDba(this.buffer, OFFSET_CACHE_HEADER);
	}

	@Override
	public void setBlockCacheDba(int dba) throws Exception {
		BlockCacheHeaderHelper.setDba(this.buffer, OFFSET_CACHE_HEADER, dba);
	}

	@Override
	public long getBlockCacheScn() throws Exception {
		return BlockCacheHeaderHelper.getScn(this.buffer, OFFSET_CACHE_HEADER);
	}

	@Override
	public void setBlockCacheScn(long scn) throws Exception {
		BlockCacheHeaderHelper.setScn(this.buffer, OFFSET_CACHE_HEADER, scn);
	}

	@Override
	public int getBlockCacheSeq() {
		return BlockCacheHeaderHelper.getSeq(this.buffer, OFFSET_CACHE_HEADER);
	}

	@Override
	public void setBLockCacheSeq(int seq) throws Exception {
		BlockCacheHeaderHelper.setSeq(this.buffer, OFFSET_CACHE_HEADER, seq);
	}

	@Override
	public byte getBlockCacheFlags() {
		return BlockCacheHeaderHelper.getFlags(this.buffer, OFFSET_CACHE_HEADER);
	}

	@Override
	public void setBlockCacheFlags(byte flags) {
		BlockCacheHeaderHelper.setFlags(this.buffer,  OFFSET_CACHE_HEADER,
				flags);
	}

	@Override
	public int getBlockCacheHeaderSize() {
		return BlockCacheHeaderHelper.getCacheHeaderSize();
	}
	
	@Override
	public void format() {
		
	}

/*= space header 구현 =========================================================*/

	@Override
	public int getFreespace() throws Exception {
		return BlockSpaceHeaderHelper.getFreespace(this.buffer,
				BlockCacheHeaderHelper.getCacheHeaderSize());
	}

	@Override
	public void setFreespace(int freespace) throws Exception {
		BlockSpaceHeaderHelper.setFreespace(this.buffer,
				BlockCacheHeaderHelper.getCacheHeaderSize(), freespace);
	}

	@Override
	public int getFreepos() throws Exception {
		return BlockSpaceHeaderHelper.getFreepos(this.buffer,
				BlockCacheHeaderHelper.getCacheHeaderSize());
	}

	@Override
	public void setFreepos(int freepos) throws Exception {
		BlockSpaceHeaderHelper.setFreepos(this.buffer,
				BlockCacheHeaderHelper.getCacheHeaderSize(), freepos);
	}
	
	@Override
	public int getSpaceHeaderSize() throws Exception {
		int offset = BlockCacheHeaderHelper.getCacheHeaderSize();
		return BlockSpaceHeaderHelper.getSpaceHeaderSize(this.buffer, offset);
	}

/*= tx header 구현 ============================================================*/
	
	@Override
	public int getTxCount() throws Exception {
		int offset = BlockCacheHeaderHelper.getCacheHeaderSize();
		offset =+
				BlockSpaceHeaderHelper.getSpaceHeaderSize(this.buffer, offset);
		return BlockTxHeaderHelper.getTxCount(this.buffer, offset);
	}

	@Override
	public void setTxCount(int cnt) throws Exception {
		int offset = BlockCacheHeaderHelper.getCacheHeaderSize();
		offset =+
				BlockSpaceHeaderHelper.getSpaceHeaderSize(this.buffer, offset);
		BlockTxHeaderHelper.setTxCount(this.buffer, offset, cnt);
	}

	@Override
	public int extendBlockTxEntry() throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int shrinkBlockTxEntry() throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public BlockTxState getBlockTxState(int i) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setBlockTxState(int i, BlockTxState state) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Xid getBlockTxId(int i) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setBlockTxid(int i, int usgmtId, int wrapNo) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public UndoAddress getUndoAddress(int i) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setUndoAddress(int i, int dba, int seqNo, int rowNo)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public long getCommitScn(int i) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setCommitScn(int i, long scn) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getCredit(int i) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setCredit(int i, int credit) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public int getTxHeaderSize() throws Exception {
		int offset = BlockCacheHeaderHelper.getCacheHeaderSize();
		offset =+
				BlockSpaceHeaderHelper.getSpaceHeaderSize(this.buffer, offset);
		return BlockTxHeaderHelper.getTxHeaderSize(this.buffer, offset);
	}

/*= AbstractBlock 구현 ========================================================*/
	
	@Override
	public void dump(PrintWriter out) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dump(PrintStream out) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dump(StringBuffer out) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int extendTxHeader(int incSize) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int shrinkTxHeader(int decSize) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}
}
